<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Zeta-Daryll
 * Date: 6/17/14
 * Time: 12:09 PM
 * To change this templates use File | Settings | File Templates.
 */
class Admin_product_model extends CI_Model {

    public function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database();
    }

    public function getTableProductManager($limit, $start){
        $this->db->select("*, pr.id AS prID, pr.description AS prDes, pr.active AS prActive");
        $this->db->from("product AS pr, category AS ca");
        $this->db->where("pr.id_category = ca.id AND pr.flag = '0' AND ca.flag = '0'");
        $this->db->order_by("pr.create_datetime", "DESC");
        $this->db->limit($limit, $start);
        $query = $this->db->get();
        if(!$query){
            $error = $this->db->_error_message();
            return $error;
        }
        else{
            if ($query->num_rows() > 0) {
                foreach ($query->result() as $row) {
                    $data[] = $row;
                }
                return $data;
            }
            return false;
        }
    }

    public function getNumOfTableProductManager(){
        $this->db->select("*, pr.id AS prID, pr.description AS prDes, pr.active AS prActive");
        $this->db->from("product AS pr, category AS ca");
        $this->db->where("pr.id_category = ca.id AND pr.flag = '0' AND ca.flag = '0'");
        $this->db->order_by("pr.create_datetime", "DESC");
        $query = $this->db->get();
        return $query->num_rows();
    }

    public function getIDProduct($field)
    {
        $query = $this->db->query("SELECT $field FROM product");
        $row = $query->row_array();
        return $row[$field];
    }

    public function insertProduct($idProduct, $productName, $productPrice, $priceUnit, $specifications, $images, $documents, $description, $id_sub_category, $idUser){
        $dbRet = $this->db->query("INSERT INTO product VALUES( '$idProduct', '$productName', '$productPrice', '$priceUnit'
            , '$specifications', '$images', '$documents', '$description', '$id_sub_category', '$idUser')");
        if( !$dbRet )
        {
            $errNo   = $this->db->_error_number();
            $errMess = $this->db->_error_message();
            return $errMess;
        }
        return 0;
    }

    public function searchCategory($searchString){
        $this->db->select('ca.name AS caName, sc.name AS scName');
        $this->db->from('product_category AS ca, product_sub_category AS sc, product AS pr, accessory AS ac');
        //$this->db->where('ca.id = sc.id_category');
        //$this->db->where('pr.id_sub_category = sc.id');
        //$this->db->where('ac.id_product = pr.id');
        $this->db->like('ca.name', $searchString);
        $this->db->or_like('sc.name', $searchString);
        $this->db->or_like('pr.product_name', $searchString);
        $this->db->or_like('ac.accessory_name', $searchString);
        $this->db->group_by('ca.id');

        $dbRet = $this->db->get();

        if(!$dbRet){
            $errMess = $this->db->_error_message();
            return $errMess;
        }
        return $dbRet->result();
    }

    public function deleteProduct($id){
        $query = $this->db->query("UPDATE product SET flag = 1 WHERE id = '$id'");
        if(!$query){
            $error = $this->db->_error_message();
            return $error;
        }
        return 0;
    }

    public function getInfoProductCode($prCode){
        $query = $this->db->query("SELECT * FROM product WHERE product_code = '$prCode'");
        return $query->row_array();
    }

    public function getRelatedProduct($id, $id_category){
        $this->db->from('product AS pr');
        $this->db->where('pr.id != ', $id);
        $this->db->where("pr.id_category", $id_category);
        $this->db->where('pr.flag = "0" AND pr.active = "1"');
        $this->db->order_by("pr.create_datetime", "DESC");
        $this->db->limit(5);
        $query = $this->db->get();
        return $query->result();
    }

    public function getAddCategoryProduct($id_category){
        $this->db->select('pr.id, pr.product_name');
        $this->db->from('product AS pr');
        $this->db->where("pr.id_category", $id_category);
        $this->db->where('pr.flag = "0" AND pr.active = "1"');
        $query = $this->db->get();
        return $query->result();
    }

    public function getAllCategoryProduct(){
        $this->db->select('pr.id, pr.product_name');
        $this->db->from('product AS pr');
        $this->db->where('pr.flag = "0" AND pr.active = "1"');
        $query = $this->db->get();
        return $query->result();
    }

    public function getIDProductByName($field, $productName){
        $query = $this->db->query("SELECT $field FROM product WHERE product_name = '$productName'");
        $row = $query->row_array();
        return $row[$field];
    }

    public function getNameProductByID($field, $productID){
        $query = $this->db->query("SELECT $field FROM product WHERE id = '$productID'");
        $row = $query->row_array();
        return $row[$field];
    }
}
?>